#include "Illumination.h"

void Illumination::apply()
{

	if (!doublesided)  glLightModelf (GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
	else  glLightModelf (GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

	if(!local) glLightModelf (GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
	else glLightModelf (GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
	
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
	glClearColor(background[0],background[1],background[2],background[3]);

	glEnable(GL_LIGHTING);

}

int Illumination::read(TiXmlElement *illumination)
{
	string test=illumination->Attribute("doublesided");
	if (test.compare("1")==0)
		doublesided=true;
	else doublesided=false;

	string test1=illumination->Attribute("local");
	if (test1.compare("1")==0)
		local=true;
	else local=false;

	TiXmlElement *aambient=illumination->FirstChildElement();
	TiXmlElement *abackground=aambient->NextSiblingElement();

	//luz ambiente
	if(aambient->QueryFloatAttribute("r", &ambient[0]) == TIXML_SUCCESS &&
		aambient->QueryFloatAttribute("g", &ambient[1]) == TIXML_SUCCESS &&
		aambient->QueryFloatAttribute("b", &ambient[2]) == TIXML_SUCCESS &&
		aambient->QueryFloatAttribute("a", &ambient[3]) == TIXML_SUCCESS)
	{
		cout<<"Iluminacao ambiente lida com sucesso"<<endl;	
	}
	else
	{
		cout<<"Iluminacao ambiente lida com erros"<<endl;
		return -1;
	}

	//cor background
	if(abackground->QueryFloatAttribute("r", &background[0]) == TIXML_SUCCESS &&
		abackground->QueryFloatAttribute("g", &background[1]) == TIXML_SUCCESS &&
		abackground->QueryFloatAttribute("b", &background[2]) == TIXML_SUCCESS &&
		abackground->QueryFloatAttribute("a", &background[3]) == TIXML_SUCCESS)
	{
		cout<<"Iluminacao background lida com sucesso"<<endl;
	}
	else
	{
		cout<<"Iluminacao background lida com erros"<<endl;
		return -1;
	}
	return 0;
}